package com.ssy.controller;

import cn.hutool.core.util.StrUtil;
import com.ssy.common.Result;
import com.ssy.service.CourseService;
import com.ssy.service.impl.CourseServiceImpl;
import com.ssy.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

/**
 * CourseServlet
 *
 * @author ssy
 * @date 2020/11/20
 **/
@WebServlet(urlPatterns = {"/course.do"})
public class CourseServlet extends HttpServlet implements Serializable {
    private static final long serialVersionUID = -8727833276050119072L;

    CourseService courseService = new CourseServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("courseList".equals(type)) {
            courseList(req, resp);
        } else if ("addCourse".equals(type)) {
            addCourse(req, resp);
        } else if ("updateCourse".equals(type)) {
            updateCourse(req, resp);
        } else if ("deleteOneCourse".equals(type)) {
            deleteOneCourse(req, resp);
        } else if ("bathDelCourse".equals(type)) {
            bathDelCourse(req, resp);
        }
    }

    /**
     * 批量删除课程
     *
     * @param req
     * @param resp
     */
    private void bathDelCourse(HttpServletRequest req, HttpServletResponse resp) {
        String[] ids = req.getParameterValues("id");
        Result result = this.courseService.BathdeleteCourse(ids);
        RespUtil.write(resp, result);
    }

    /**
     * 删除一个
     *
     * @param req
     * @param resp
     */
    private void deleteOneCourse(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Result result = this.courseService.deleteOneCourse(id);
        RespUtil.write(resp, result);
    }

    /**
     * 修改
     *
     * @param req
     * @param resp
     */
    private void updateCourse(HttpServletRequest req, HttpServletResponse resp) {
        String courseId = req.getParameter("courseId");
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String classHours = req.getParameter("classHours");
        String credit = req.getParameter("credit");

        Result rs = this.courseService.updateCourse(courseId, number, name, classHours, credit);
        RespUtil.write(resp, rs);
    }

    /**
     * 添加课程信息
     *
     * @param req
     * @param resp
     */
    private void addCourse(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String classHours = req.getParameter("classHours");
        String credit = req.getParameter("credit");

        Result rs = this.courseService.addCourse(number, name, classHours, credit);
        RespUtil.write(resp, rs);
    }


    /**
     * 加载课程列表
     *
     * @param req
     * @param resp
     */
    private void courseList(HttpServletRequest req, HttpServletResponse resp) {
        String number = req.getParameter("number");
        String name = req.getParameter("name");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        HashMap<String, Object> param = new HashMap<>();
        if (StrUtil.isNotBlank(number)) {
            param.put("number", number);
        }
        if (StrUtil.isNotBlank(name)) {
            param.put("name", name);
        }
        Result result = this.courseService.queryPage(param, page, limit);
        RespUtil.write(resp, result);
    }


}
